package MainDemo;/**
 * @author XuXiaoK
 * @create 2020-01-21 14:16
 */

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

/**
 * @Author XiaoKe
 * @Date 2020/1/21 14:16
 * @Description TODO
 */
public class FallbackTest {

    public static void main(String[] args) {
        // 断路器被强制打开
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.circuitBreaker.forceOpen", "true");
        FallbackCmmand fallbackCmmand = new FallbackCmmand();
        fallbackCmmand.execute();

        // 断路器关闭
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.circuitBreaker.forceOpen", "false");
        FallbackCmmand fallbackCmmand2 = new FallbackCmmand();
        fallbackCmmand2.execute();
    }

    static class FallbackCmmand extends HystrixCommand<String> {

        public FallbackCmmand() {
            super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));

        }

        protected String run() throws Exception {
            System.out.println("命令执行");
            return "";
        }

        @Override
        protected String getFallback() {
            System.out.println("执行回退方法");
            return "fallback";
        }
    }

}
